/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.db.generator;

import net.db.generator.utils.ClassGeneratorUtil;
import net.db.generator.field.models.BaseFieldModel;

/**
 *
 * @author abosancic
 */
public class JavaDAOBaseClassWriter extends JavaBaseClassWriter{
    
    //  DTO  = Data Transfer Object
    //  DAO  = Data Access Object
    //  DCO  = Dynamically Configurable Object

    public static String DAO_PREFIX = "DAO_";
    
    protected String dtoPackageName;
    
    public JavaDAOBaseClassWriter() {
        super();
    }
    
    @Override
    protected void generateClassName() {
        String value = getPrimaryKeyType();
        stringClass.append("public class ").append(ClassGeneratorUtil.humanize(getClassName())).append(" extends DAOBase<" + ClassGeneratorUtil.humanize(super.getClassName()) + ", "+ value +"> {\n\n");
    }
    
//    @Override
//    protected void generateConstructor(){
//        stringClass.append(spaces).append("public ").append(ClassGeneratorUtil.humanize(getClassName())).append("() {\n");
//        stringClass.append(spaces).append(spaces).append("super(").append(ClassGeneratorUtil.humanize(super.getClassName())).append(".class").append(");\n");;
//        stringClass.append(spaces).append("}\n\n");
//    }
    
    //==========================================================================
    // setters and getters
    //==========================================================================
    @Override
    public String getClassName() {
        return DAO_PREFIX + super.getClassName();
    }

    @Override
    protected void generateGettersAndSetters() {
        stringClass.append(spaces).append("@Override\n");
        stringClass.append(spaces).append("protected String [] getColumns(){\n");
        
        String column = "";
        String keys = "";
        
        for (BaseFieldModel fieldModel : fields) {
            column += "\"" + fieldModel.getFieldName() + "\",";
            
            if (fieldModel.isPrimaryKey()) {
                keys += "\"" + fieldModel.getFieldName() + "\",";
            }
        }
        
        if (column.length() > 1) {
            stringClass.append(spaces).append(spaces).append("return new String[]{").append(column.substring(0, column.length() - 1)).append("};\n");
        } else {
            stringClass.append(spaces).append(spaces).append("return new String[]{};\n");
        }
        stringClass.append(spaces).append("}\n\n");
        
        stringClass.append(spaces).append("@Override\n");
        stringClass.append(spaces).append("protected String [] getPrimaryKeyName(){\n");

        if (keys.length() > 1) {
            stringClass.append(spaces).append(spaces).append("return new String[]{").append(keys.substring(0, keys.length() - 1)).append("};\n");
        } else {
            stringClass.append(spaces).append(spaces).append("return new String[]{};\n");
        }
        stringClass.append(spaces).append("}\n\n");
        
        stringClass.append(spaces).append("@Override\n");
        stringClass.append(spaces).append("protected String getTableName(){\n");
        stringClass.append(spaces).append(spaces).append("return \"" + super.getClassName() + "\";\n");
        stringClass.append(spaces).append("}\n\n");

    }

    @Override
    protected void generateFields() {
        
    }

    @Override
    protected void generateToString() {
        
    }

    @Override
    protected void generateHashCode() {
        
    }

    @Override
    protected void generateEquals() {
     
    }

    public String getPrimaryKeyType() {
        
        String keyType = null;
        
        if (uniqueField.length > 1) {
            keyType = ClassGeneratorUtil.humanize(super.getClassName()) + "PK";
        } else {
            for (BaseFieldModel fieldModel : fields) {
                if (fieldModel.isPrimaryKey()) {
                    return fieldModel.getFieldType();
                }
            }
        }
        
        return keyType;
    }
}
